import ws from "ws";
import msgpack from "msgpack-lite";

export class agent {
    private socket: ws;

    [x: string]: any;
    id: number = 0;

    ip: string = "";

    constructor(s: ws) {
        this.socket = s;
    }

    onClose(code: number, reason: string) {

    }

    send(msg: number | string | Buffer | Uint8Array | ArrayBuffer): void;
    send(msg: number | string | Buffer | Uint8Array | ArrayBuffer, param: any): void;
    send(msg: number | string | Buffer | Uint8Array | ArrayBuffer, param: any = null) {
        if (param == null) {
            this.socket.send(msg, { compress: true });
            return;
        }
        if (typeof msg == "number") {
            let buffer = msgpack.encode({
                c: msg,
                d: param,
            });
            this.socket.send(buffer);
            return;
        }
        console.error("[agent] 发送类型错误", msg, param);
    }

    userId: number = 0;
    bind(userId: number) {
        this.userId = userId;
    }
}